#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QListView>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QList>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  // this->setWindowFlags(Qt::FramelessWindowHint);
  this->config = new Config();

  // 串口号选项
  this->ui->port_list->setEditable(true);
  this->ui->port_list->setView(new QListView);
  foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    this->ui->port_list->addItem(info.portName());
  }

  // 波特率选项
  this->ui->baud_list->setEditable(true);
  foreach (const uint32_t &baud, MainWindow::baudList) {
    this->ui->baud_list->addItem(QString::number(baud));
  }
  this->ui->baud_list->setCurrentText("115200");
}

MainWindow::~MainWindow() { delete ui; }

/**
 * @brief MainWindow::on_clear_all_clicked
 * 清空按钮点击事件
 */
void MainWindow::on_clear_all_clicked(void) {
  this->ui->output_edit->clear();
  this->ui->send_edit->clear();
}

void MainWindow::on_open_port_clicked(void) {
  this->config->port = this->ui->port_list->currentText();
  this->config->baud = this->ui->baud_list->currentText().toUInt();
  this->config->stop_bit = this->ui->stop_bit->currentText();
  this->config->data_bit = this->ui->data_bit->currentText();
  this->config->parity_bit = this->ui->parity_bit->currentText();
  this->config->cts = this->ui->cb_cts->isChecked();
  this->config->rts = this->ui->cb_rts->isChecked();
}

/**
 * @brief on_about_app_triggered
 * 关于软件 选项事件
 */
void MainWindow::on_about_app_triggered() {
  QMessageBox msgBox;
  msgBox.setWindowTitle("关于软件");
  msgBox.setText(R"EOF(****************************
这是一个简易的串口调试助手
****************************
)EOF");
  QPushButton *okButton = msgBox.addButton(QMessageBox::Ok);
  okButton->setIcon(QIcon());
  msgBox.exec();
}
